package financial.sharing.center.system.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.utils.StringUtil;
import financial.sharing.center.system.dao.TaxRateMapper;
import financial.sharing.center.system.model.entity.TaxRate;
import financial.sharing.center.system.service.TaxRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TaxRateServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/5/6
 * @Version 1.0
 */
@Service
public class TaxRateServiceImpl implements TaxRateService {

    @Autowired
    private TaxRateMapper taxRateMapper;

    @Override
    public PageInfo<TaxRate> getAllTexRate(String selectTaxRegion, String selectTaxType) {
        Example example = new Example(TaxRate.class);
        example.createCriteria().andLike("taxRegion", StringUtil.toLike(selectTaxRegion))
                                .andLike("taxType",StringUtil.toLike(selectTaxType))
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        List<TaxRate> taxRateList = this.taxRateMapper.selectByExample(example);
        return new PageInfo<>(taxRateList);
    }

    private TaxRate getTaxRate(Integer id){
        return this.taxRateMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer createOneTaxRate(String taxRegion, String taxType, BigDecimal rate) {
        TaxRate taxRate = new TaxRate(taxRegion, taxType, rate);
        if (this.getTaxRate(taxRate) != null){
            return ResponseContext.已经存在.getValue();
        }
        int i = this.taxRateMapper.insert(taxRate);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOneTaxRate(TaxRate taxRate) {
        if (this.getTaxRate(taxRate) != null){
            return ResponseContext.已经存在.getValue();
        }
        int i = this.taxRateMapper.updateByPrimaryKey(taxRate);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    private TaxRate getTaxRate(TaxRate taxRate) {
        Example example = new Example(TaxRate.class);
        example.createCriteria().andEqualTo("taxRegion",taxRate.getTaxRegion())
                                .andEqualTo("taxType",taxRate.getTaxType())
                                .andEqualTo("rate",taxRate.getRate())
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        return this.taxRateMapper.selectOneByExample(example);
    }

    @Override
    public Integer deleteOneTaxRate(Integer id) {
        TaxRate taxRate = this.getTaxRate(id);
        taxRate.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.taxRateMapper.updateByPrimaryKey(taxRate);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public Map<Integer, BigDecimal> getTaxRateMap() {
        List<TaxRate> listTaxRate = this.getListTaxRate();
        Map<Integer, BigDecimal> taxRateMap = new HashMap<>();
        for (TaxRate taxRate : listTaxRate) {
            taxRateMap.put(taxRate.getId(), taxRate.getRate());
        }
        return taxRateMap;
    }

    @Override
    public List<TaxRate> getListTaxRate() {
        Example example = new Example(TaxRate.class);
        example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        List<TaxRate> taxRateList = this.taxRateMapper.selectByExample(example);
        return taxRateList;
    }
}
